#!/bin/bash

datDir=$1 #./data

ls ${datDir} | grep -v -E "reviews.txt|requests.txt" > user_list

sed -e 's/\.txt$//' user_list > tmp

echo "user,total_reviews,total_requests,submitted_requests,discared_requests,pending_requests" > alluser_RR_stat.res
echo "revier,month,reviewCnt" > reviewCnt_perMonth.res

#the following three files will be changed by the requestCntPerMonth_user.py
echo "status,submitter,request_id,created_at,updated_at,group" >formatted_user_requests
echo "user,created_month,requestCntPerCreatedMon" >user_requestCnt_created.res
echo "user,updated_month,requestCntPerUpdatedMon" >user_requestCnt_updated.res

for u in $(cat tmp)
do
    #this cmd can let us know whether the output is all valid, and obtain potential missing users' reviews and requests.
    #cat ${datDir}/${u}_reviews.txt | awk -F "邹" '{print NF}' > check_r 2>&1
    #cat ${datDir}/${u}_requests.txt | awk -F "邹" '{print NF}' > check_r 2>&1

    u_reviews=$(cat ${datDir}/${u}_reviews.txt | wc -l)

    u_requests=$(cat ${datDir}/${u}_requests.txt | wc -l)
    u_submitted_r=$(cat ${datDir}/${u}_requests.txt | grep -E "^Submitted" | wc -l)
    u_discarded_r=$(cat ${datDir}/${u}_requests.txt | grep -E "^Discarded" | wc -l)
    u_pending_r=$((u_requests-u_submitted_r-u_discarded_r))
    
    python reviewCntPerMonth_user.py ${datDir}/${u}_reviews.txt $u >> reviewCnt_perMonth.res
    python requestCntPerMonth_user.py ${datDir}/${u}_requests.txt

    echo $u,$u_reviews,$u_requests,$u_submitted_r,$u_discarded_r,$u_pending_r >> alluser_RR_stat.res
done

    awk -F "," '{if($2 !=0 || $3!=0) print $0}' alluser_RR_stat.res > hasRecord_users.res
